﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pairingproject2._3.DefinitionS
{
   public class Definition
    {
        //插入括号模块--------------------------------------//完成，有小许Bug等待修复
        //Random表示伪随机数生成器
        Random random = new Random();
        string[] brackets = new string[2] { "(", ")" };//括号字符串
        int[] matchCondition=new int[2] { 1000,1000};       //保存可以插入字符串的位置;默认是1000预防出错
        string sum = "";
        public Random Random { get => random; set => random = value; }
        public string[] Brackets { get => brackets; set => brackets = value; }
        public int[] MatchCondition { get => matchCondition; set => matchCondition = value; }
        public string Sum { get => sum; set => sum = value; }

        private void Select()//找出插入位置
        {
            int sumlengths = Sum.Length;//获得题目可插入位置的长度
            int bracketslength = Brackets.Length;//记录剩余几个位置为选出
            int record;
            int toInsert=0;//记录已经获取插入下标的数量
            while (bracketslength!=0)
            {
                if (toInsert == 0)//麻烦，第一个括号插入的位置只能是偶数，第二个只能是奇数
                {
                    record = Random.Next(0, sumlengths-1);//随机获取位置来插入符号
                    if (record % 2 == 0)
                    {
                        MatchCondition[Brackets.Length - bracketslength] = record;//brackets.Length- bracketslength是获得插入下标
                        bracketslength--;
                        if (MatchCondition[0] == MatchCondition[1])                //防止插入位置相同
                        {
                            bracketslength++;//位置符合条件，
                        }
                        toInsert++;
                    }
                }
               else if (toInsert == 1)
                {
                    record = Random.Next(MatchCondition[0], sumlengths);//随机获取位置来插入符号
                    if (record % 2 != 0)
                    {
                        MatchCondition[Brackets.Length - bracketslength] = record;//brackets.Length- bracketslength是获得插入下标
                        bracketslength--;
                        if (MatchCondition[0] == MatchCondition[1])                //防止插入位置相同
                        {
                            bracketslength++;//位置符合条件，
                        }
                    }
                }
            }
            if(MatchCondition[0]> MatchCondition[1])//调整位置大小，方便插入
            {
                int num = MatchCondition[0];
                MatchCondition[0] = MatchCondition[1];
                MatchCondition[1] = num;
            }
        }
        public string InsertTheBrackets(string value)//插入字符串
        {
            Sum = value;
            Select();//找出插入位置
            for (int i = 0; i < 2; i++)
            {
                Sum = Sum.Insert(MatchCondition[i]+i, Brackets[i]);
            }
            return Sum;
        }
    }
}
